/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2024.
 * Description: rtos spinlock enhance for arm and ppc.
 * enable CONFIG_RTOS_LOCK_STATE_CHANGE_CHECK will compile it
 * Author: Wang Haitao
 * Create: 2023-9-14
 */

#include <linux/spinlock.h>
#include <linux/export.h>

#ifdef CONFIG_RTOS_LOCK_STATE_CHANGE_CHECK
#include <linux/rtos_dump.h>
#endif

#if defined(CONFIG_RTOS_RWLOCK_STATE_CHANGE_CHECK) && defined(CONFIG_ARM)
#define WRLOCK_TOKEN 0x80000000
#endif


#ifdef CONFIG_RTOS_LOCK_STATE_CHANGE_CHECK
static inline void print_lock_addr(unsigned long lock)
{
	pr_err("lock addr:%lx\n", lock);
}
#endif

#ifdef CONFIG_RTOS_SPINLOCK_STATE_CHANGE_CHECK
static inline void print_slock_info(raw_spinlock_t *lock)
{
	print_lock_addr((unsigned long)&lock->raw_lock.slock);
	rtos_dump_lock_mem((unsigned long)&lock->raw_lock.slock);
	BUG();
}

#ifndef CONFIG_INLINE_SPIN_TRYLOCK
int __lockfunc _raw_spin_trylock(raw_spinlock_t *lock)
{
	int ret = __raw_spin_trylock(lock);
	if (ret && unlikely(lock->raw_lock.slock != LOCK_TOKEN))
		print_slock_info(lock);
	return ret;
}
EXPORT_SYMBOL(_raw_spin_trylock);
#endif

#ifndef CONFIG_INLINE_SPIN_TRYLOCK_BH
int __lockfunc _raw_spin_trylock_bh(raw_spinlock_t *lock)
{
	int ret = __raw_spin_trylock_bh(lock);
	if (ret && unlikely(lock->raw_lock.slock != LOCK_TOKEN))
		print_slock_info(lock);
	return ret;
}
EXPORT_SYMBOL(_raw_spin_trylock_bh);
#endif

#ifndef CONFIG_INLINE_SPIN_LOCK
void __lockfunc _raw_spin_lock(raw_spinlock_t *lock)
{
	__raw_spin_lock(lock);
	if (unlikely(lock->raw_lock.slock != LOCK_TOKEN))
		print_slock_info(lock);
}
EXPORT_SYMBOL(_raw_spin_lock);
#endif

#ifndef CONFIG_INLINE_SPIN_LOCK_IRQSAVE
unsigned long __lockfunc _raw_spin_lock_irqsave(raw_spinlock_t *lock)
{
	unsigned long ret = __raw_spin_lock_irqsave(lock);
	if (unlikely(lock->raw_lock.slock != LOCK_TOKEN))
		print_slock_info(lock);
	return ret;
}
EXPORT_SYMBOL(_raw_spin_lock_irqsave);
#endif

#ifndef CONFIG_INLINE_SPIN_LOCK_IRQ
void __lockfunc _raw_spin_lock_irq(raw_spinlock_t *lock)
{
	__raw_spin_lock_irq(lock);
	if (unlikely(lock->raw_lock.slock != LOCK_TOKEN))
		print_slock_info(lock);
}
EXPORT_SYMBOL(_raw_spin_lock_irq);
#endif

#ifndef CONFIG_INLINE_SPIN_LOCK_BH
void __lockfunc _raw_spin_lock_bh(raw_spinlock_t *lock)
{
	__raw_spin_lock_bh(lock);
	if (unlikely(lock->raw_lock.slock != LOCK_TOKEN))
		print_slock_info(lock);
}
EXPORT_SYMBOL(_raw_spin_lock_bh);
#endif

#ifdef CONFIG_UNINLINE_SPIN_UNLOCK
void __lockfunc _raw_spin_unlock(raw_spinlock_t *lock)
{
	if (unlikely(lock->raw_lock.slock != LOCK_TOKEN))
		print_slock_info(lock);
	__raw_spin_unlock(lock);
}
EXPORT_SYMBOL(_raw_spin_unlock);
#endif

#ifndef CONFIG_INLINE_SPIN_UNLOCK_IRQRESTORE
void __lockfunc _raw_spin_unlock_irqrestore(raw_spinlock_t *lock, unsigned long flags)
{
	if (unlikely(lock->raw_lock.slock != LOCK_TOKEN))
		print_slock_info(lock);
	__raw_spin_unlock_irqrestore(lock, flags);
}
EXPORT_SYMBOL(_raw_spin_unlock_irqrestore);
#endif

#ifndef CONFIG_INLINE_SPIN_UNLOCK_IRQ
void __lockfunc _raw_spin_unlock_irq(raw_spinlock_t *lock)
{
	if (unlikely(lock->raw_lock.slock != LOCK_TOKEN))
		print_slock_info(lock);
	__raw_spin_unlock_irq(lock);
}
EXPORT_SYMBOL(_raw_spin_unlock_irq);
#endif

#ifndef CONFIG_INLINE_SPIN_UNLOCK_BH
void __lockfunc _raw_spin_unlock_bh(raw_spinlock_t *lock)
{
	if (unlikely(lock->raw_lock.slock != LOCK_TOKEN))
		print_slock_info(lock);
	__raw_spin_unlock_bh(lock);
}
EXPORT_SYMBOL(_raw_spin_unlock_bh);
#endif
#endif // CONFIG_RTOS_SPINLOCK_STATE_CHANGE_CHECK

#ifdef CONFIG_RTOS_RWLOCK_STATE_CHANGE_CHECK
static inline void print_lock_info(rwlock_t *lock)
{
	print_lock_addr((unsigned long)&lock->raw_lock.lock);
	rtos_dump_lock_mem((unsigned long)&lock->raw_lock.lock);
	BUG();
}

#ifndef CONFIG_INLINE_READ_TRYLOCK
int __lockfunc _raw_read_trylock(rwlock_t *lock)
{
	int ret = __raw_read_trylock(lock);
	if (ret && unlikely(lock->raw_lock.lock == 0))
		print_lock_info(lock);
	return ret;
}
EXPORT_SYMBOL(_raw_read_trylock);
#endif

#ifndef CONFIG_INLINE_READ_LOCK
void __lockfunc _raw_read_lock(rwlock_t *lock)
{
	__raw_read_lock(lock);
	if (unlikely(lock->raw_lock.lock == 0))
		print_lock_info(lock);
}
EXPORT_SYMBOL(_raw_read_lock);
#endif

#ifndef CONFIG_INLINE_READ_LOCK_IRQSAVE
unsigned long __lockfunc _raw_read_lock_irqsave(rwlock_t *lock)
{
	unsigned long ret = __raw_read_lock_irqsave(lock);
	if (unlikely(lock->raw_lock.lock == 0))
		print_lock_info(lock);
	return ret;
}
EXPORT_SYMBOL(_raw_read_lock_irqsave);
#endif

#ifndef CONFIG_INLINE_READ_LOCK_IRQ
void __lockfunc _raw_read_lock_irq(rwlock_t *lock)
{
	__raw_read_lock_irq(lock);
	if (unlikely(lock->raw_lock.lock == 0))
		print_lock_info(lock);
}
EXPORT_SYMBOL(_raw_read_lock_irq);
#endif

#ifndef CONFIG_INLINE_READ_LOCK_BH
void __lockfunc _raw_read_lock_bh(rwlock_t *lock)
{
	__raw_read_lock_bh(lock);
	if (unlikely(lock->raw_lock.lock == 0))
		print_lock_info(lock);
}
EXPORT_SYMBOL(_raw_read_lock_bh);
#endif

#ifndef CONFIG_INLINE_READ_UNLOCK
void __lockfunc _raw_read_unlock(rwlock_t *lock)
{
	if (unlikely(lock->raw_lock.lock == 0))
		print_lock_info(lock);
	__raw_read_unlock(lock);
}
EXPORT_SYMBOL(_raw_read_unlock);
#endif

#ifndef CONFIG_INLINE_READ_UNLOCK_IRQRESTORE
void __lockfunc _raw_read_unlock_irqrestore(rwlock_t *lock, unsigned long flags)
{
	if (unlikely(lock->raw_lock.lock == 0))
		print_lock_info(lock);
	__raw_read_unlock_irqrestore(lock, flags);
}
EXPORT_SYMBOL(_raw_read_unlock_irqrestore);
#endif

#ifndef CONFIG_INLINE_READ_UNLOCK_IRQ
void __lockfunc _raw_read_unlock_irq(rwlock_t *lock)
{
	if (unlikely(lock->raw_lock.lock == 0))
		print_lock_info(lock);
	__raw_read_unlock_irq(lock);
}
EXPORT_SYMBOL(_raw_read_unlock_irq);
#endif

#ifndef CONFIG_INLINE_READ_UNLOCK_BH
void __lockfunc _raw_read_unlock_bh(rwlock_t *lock)
{
	if (unlikely(lock->raw_lock.lock == 0))
		print_lock_info(lock);
	__raw_read_unlock_bh(lock);
}
EXPORT_SYMBOL(_raw_read_unlock_bh);
#endif

#ifndef CONFIG_INLINE_WRITE_TRYLOCK
int __lockfunc _raw_write_trylock(rwlock_t *lock)
{
	int ret = __raw_write_trylock(lock);
	if (ret && unlikely(lock->raw_lock.lock != WRLOCK_TOKEN))
		print_lock_info(lock);
	return ret;
}
EXPORT_SYMBOL(_raw_write_trylock);
#endif

#ifndef CONFIG_INLINE_WRITE_LOCK
void __lockfunc _raw_write_lock(rwlock_t *lock)
{
	__raw_write_lock(lock);
	if (unlikely(lock->raw_lock.lock != WRLOCK_TOKEN))
		print_lock_info(lock);
}
EXPORT_SYMBOL(_raw_write_lock);
#endif

#ifndef CONFIG_INLINE_WRITE_LOCK_IRQSAVE
unsigned long __lockfunc _raw_write_lock_irqsave(rwlock_t *lock)
{
	unsigned long ret = __raw_write_lock_irqsave(lock);
	if (unlikely(lock->raw_lock.lock != WRLOCK_TOKEN))
		print_lock_info(lock);
	return ret;
}
EXPORT_SYMBOL(_raw_write_lock_irqsave);
#endif

#ifndef CONFIG_INLINE_WRITE_LOCK_IRQ
void __lockfunc _raw_write_lock_irq(rwlock_t *lock)
{
	__raw_write_lock_irq(lock);
	if (unlikely(lock->raw_lock.lock != WRLOCK_TOKEN))
		print_lock_info(lock);
}
EXPORT_SYMBOL(_raw_write_lock_irq);
#endif

#ifndef CONFIG_INLINE_WRITE_LOCK_BH
void __lockfunc _raw_write_lock_bh(rwlock_t *lock)
{
	__raw_write_lock_bh(lock);
	if (unlikely(lock->raw_lock.lock != WRLOCK_TOKEN))
		print_lock_info(lock);
}
EXPORT_SYMBOL(_raw_write_lock_bh);
#endif

#ifndef CONFIG_INLINE_WRITE_UNLOCK
void __lockfunc _raw_write_unlock(rwlock_t *lock)
{
	if (unlikely(lock->raw_lock.lock != WRLOCK_TOKEN))
		print_lock_info(lock);
	__raw_write_unlock(lock);
}
EXPORT_SYMBOL(_raw_write_unlock);
#endif

#ifndef CONFIG_INLINE_WRITE_UNLOCK_IRQRESTORE
void __lockfunc _raw_write_unlock_irqrestore(rwlock_t *lock, unsigned long flags)
{
	if (unlikely(lock->raw_lock.lock != WRLOCK_TOKEN))
		print_lock_info(lock);
	__raw_write_unlock_irqrestore(lock, flags);
}
EXPORT_SYMBOL(_raw_write_unlock_irqrestore);
#endif

#ifndef CONFIG_INLINE_WRITE_UNLOCK_IRQ
void __lockfunc _raw_write_unlock_irq(rwlock_t *lock)
{
	if (unlikely(lock->raw_lock.lock != WRLOCK_TOKEN))
		print_lock_info(lock);
	__raw_write_unlock_irq(lock);
}
EXPORT_SYMBOL(_raw_write_unlock_irq);
#endif

#ifndef CONFIG_INLINE_WRITE_UNLOCK_BH
void __lockfunc _raw_write_unlock_bh(rwlock_t *lock)
{
	if (unlikely(lock->raw_lock.lock != WRLOCK_TOKEN))
		print_lock_info(lock);
	__raw_write_unlock_bh(lock);
}
EXPORT_SYMBOL(_raw_write_unlock_bh);
#endif
#endif // CONFIG_RTOS_RWLOCK_STATE_CHANGE_CHECK